﻿@model MaintenanceListModel
@using Nop.Core.Domain.XJ
@using Nop.Services.Security
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    var permissionService = EngineContext.Current.Resolve<IPermissionService>();
    var canManageMaintenancesView = permissionService.Authorize(StandardPermissionProvider.ManageMaintenancesView);
    var canManageInspectionPlansView = permissionService.Authorize(StandardPermissionProvider.ManageInspectionPlansView);

    //page title
    ViewBag.Title = "维修记录列表";
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Maintenance List");
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            维修记录列表
        </h1>
        <div class="pull-right">
            <button class="btn btn-success" type="submit" name="exportmaintenanceexcel-all">
                <i class="fa fa-file-excel-o"></i>
                @T("Admin.Common.ExportToExcel.All")
            </button>
        </div>
    </div>

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default panel-search">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchSNo" title="">维修编号</label></div>
                                        <div class="col-content">
                                            @Html.NopEditorFor(model => model.SearchSNo)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchPNo" title="">计划编号</label></div>
                                        <div class="col-content">
                                            @Html.NopEditorFor(model => model.SearchPNo)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchPlaceName" title="">巡检点</label></div>
                                        <div class="col-content">
                                            @Html.NopEditorFor(model => model.SearchPlaceName)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchCategoryName" title="">设备分类</label></div>
                                        <div class="col-content">
                                            @Html.NopEditorFor(model => model.SearchCategoryName)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchEquipmentName" title="">设备名称</label></div>
                                        <div class="col-content">
                                            @Html.NopEditorFor(model => model.SearchEquipmentName)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        @Html.NopLabelFor(model => model.SearchCreatedFrom)
                                        <div class="col-content">
                                            @Html.NopEditorFor(model => model.SearchCreatedFrom)
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper" style="flex:2;"><label class="control-label" for="SearchDepartId" title="">发起部门</label></div>
                                        <div class="col-flex4">
                                            @Html.NopDropDownListFor(model => model.SearchDepartId, Model.AvailableDepartments)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper" style="flex:2;"><label class="control-label" for="SearchUserId" title="">发起人</label></div>
                                        <div class="col-flex4">
                                            @Html.NopDropDownListFor(model => model.SearchUserId, Model.AvailableUsers)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper" style="flex:2;"><label class="control-label" for="SearchResponsibleDepartmentId" title="">责任班组</label></div>
                                        <div class="col-flex4">
                                            @Html.EditorFor(model => model.SearchResponsibleDepartmentId, new { SelectList = Model.AvailableDepartmentMenus })
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchResponsiblePersonId" title="">责任人</label></div>
                                        <div class="col-content">
                                            @Html.NopDropDownListFor(model => model.SearchResponsiblePersonId, Model.AvailableUsers)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchMaintenancePersonId" title="">维修人</label></div>
                                        <div class="col-content">
                                            @Html.NopDropDownListFor(model => model.SearchMaintenancePersonId, Model.AvailableUsers)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        @Html.NopLabelFor(model => model.SearchCreatedTo)
                                        <div class="col-content">
                                            @Html.NopEditorFor(model => model.SearchCreatedTo)
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchIsExternalId" title="">是外援实施</label></div>
                                        <div class="col-content">
                                            @Html.NopDropDownListFor(model => model.SearchIsExternalId, Model.AvailableIsExternals)
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-search">
                                        <div class="label-wrapper"><label class="control-label" for="SearchState" title="">状态</label></div>
                                        <div class="col-content">
                                            @Html.NopDropDownListFor(model => model.SearchState, Model.AvailableStates)
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                    </div>
                                    <div class="col-md-2" style="text-align:right;">
                                        <button type="button" id="search-categories" class="btn btn-primary"><i class="fa fa-search"></i>@T("Admin.Common.Search")</button>
                                        <button type="button" id="cancel-categories" class="btn btn-default"><i class="fa fa-refresh"></i>重置</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="organizations-grid"></div>

                        <script>
                        $(document).ready(function () {
                            var columns = [{
                                field: "SNo",
                                title: "维修编号",
                                width: 150,
                                locked: true,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '#if(@(canManageMaintenancesView?1:0)== 1){# <a href="View/#=Id#">#=SNo#</a> #}else{# #=SNo# #}#'
                            }, {
                                field: "InspectionPlanId",
                                title: "计划编号",
                                width: 150,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '#if(@(canManageInspectionPlansView?1:0)== 1&&InspectionPlanId>0){# <a target="_blank" href="@Url.Content("~/Admin/InspectionPlan/View/")#=InspectionPlanId#">#=PNo#</a> #}else if(InspectionPlanId>0){# #=PNo# #}#'
                            }, {
                                field: "PlaceName",
                                title: "巡检点",
                                width: 200,
                                headerAttributes: { style: "text-align:center" },
                            }, {
                                field: "CategoryName",
                                title: "设备分类",
                                width: 200,
                                headerAttributes: { style: "text-align:center" },
                            }, {
                                field: "EquipmentNames",
                                title: "设备名称",
                                width: 200,
                                headerAttributes: { style: "text-align:center" },
                            },  {
                                field: "Comment",
                                title: "情况说明",
                                width: 300,
                                headerAttributes: { style: "text-align:center" },
                            }, {
                                field: "DepartmentName",
                                title: "发起部门",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "CustomerName",
                                title: "发起人",
                                width: 80,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "ResponsibleDepartmentName",
                                title: "责任部门",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            },  {
                                field: "ResponsibleTeamName",
                                title: "责任班组",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "ResponsiblePersonName",
                                title: "责任人",
                                width: 80,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "MaintenancePersonNames",
                                title: "维修人",
                                width: 200,
                                headerAttributes: { style: "text-align:center" },
                            }, {
                                field: "IsExternal",
                                title: "是外援实施",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '# if(IsExternal) {# <i class="fa fa-check true-icon"></i> #} #'
                            },  {
                                field: "Total",
                                title: "维修费用",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "CreatedOn",
                                title: "开始时间",
                                width: 120,
                                type: "date",
                                format: "{0:yy/MM/dd HH:mm}",
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "MaintenanceOn",
                                title: "完工时间",
                                width: 120,
                                type: "date",
                                format: "{0:yy/MM/dd HH:mm}",
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "HoursString",
                                title: "工时",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }, {
                                field: "Star",
                                title: "评分",
                                width: 80,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '# if(Star<90) {# <span style="color:red">#=Star#</span> #} else {# #=Star# #} #'
                            }, {
                                field: "Evaluation",
                                title: "评价内容",
                                width: 300,
                                headerAttributes: { style: "text-align:center" },
                            }, {
                                field: "StateName",
                                title: "状态",
                                width: 80,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                            }];
                        if (@(canManageMaintenancesView?1:0)== 1) {
                            columns.push({
                                field: "Id",
                                title: "@T("Admin.Common.Edit")",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '<i class="fa fa-eye fa-active" title="查看" onclick="javascript:window.location.href=\'View/#=Id#\';"></i>',
                            });
                            }

                            $("#organizations-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("MaintenanceList", "Maintenance"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: additionalData
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors"
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)],
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: true, // 滚动
                                resizable: true,  // 缩放
                                columnMenu: true, // 显示列菜单，用户可以从中选择显示或隐藏列
                                columns: columns
                            });

                            $("#@Html.FieldIdFor(model => model.SearchDepartId)").change(function () {
                                var selectedItem = $(this).val();
                                var ddlStates = $("#@Html.FieldIdFor(model => model.SearchUserId)");
                                $.ajax({
                                    cache: false,
                                    type: "GET",
                                    url: "@(Url.Action("GetUsersByDepartmentId", "Customer"))",
                                    data: { "departId": selectedItem },
                                    success: function(data) {
                                        ddlStates.html('');
                                        $.each(data, function(id, option) {
                                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                                        });
                                    },
                                    error: function(xhr, ajaxOptions, thrownError) {
                                        alert('Failed to retrieve states.');
                                    }
                                });
                            });
                            $("#@Html.FieldIdFor(model => model.SearchResponsibleDepartmentId)").change(function () {
                                var selectedItem = $(this).val();
                                var ddlStates = $("#@Html.FieldIdFor(model => model.SearchMaintenancePersonId)");
                                $.ajax({
                                    cache: false,
                                    type: "GET",
                                    url: "@(Url.Action("GetUsersByDepartmentId", "Customer"))",
                                    data: { "departId": selectedItem },
                                    success: function(data) {
                                        ddlStates.html('');
                                        $.each(data, function(id, option) {
                                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                                        });
                                    },
                                    error: function(xhr, ajaxOptions, thrownError) {
                                        alert('Failed to retrieve states.');
                                    }
                                });
                            });
                        });
                        </script>
                        <script type="text/javascript">
                        $(document).ready(function() {
                            //search button
                            $('#search-categories').click(function() {
                                //search
                                var grid = $('#organizations-grid').data('kendoGrid');
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });
                            //cancel button
                            $('#cancel-categories').click(function () {
                                //cancel
                                var grid = $('#organizations-grid').data('kendoGrid');
                                $('#@Html.FieldIdFor(model => model.SearchCreatedFrom)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchCreatedTo)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchPNo)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchSNo)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchPlaceName)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchCategoryName)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchEquipmentName)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchDepartId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchUserId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchResponsibleDepartmentId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchResponsiblePersonId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchMaintenancePersonId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchIsExternalId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchState)').val('0');
                                $("#select_menuSearchDepartId").attr("value", "全部");
                                $("#select_menuSearchResponsibleDepartmentId").attr("value", "全部");
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });

                            $("#@Html.FieldIdFor(model => model.SearchPNo)").keydown(function(event) {
                                if (event.keyCode == 13) {
                                    $("#search-categories").click();
                                    return false;
                                }
                            });

                            $("#@Html.FieldIdFor(model => model.SearchSNo)").keydown(function(event) {
                                if (event.keyCode == 13) {
                                    $("#search-categories").click();
                                    return false;
                                }
                            });

                            $("#@Html.FieldIdFor(model => model.SearchPlaceName)").keydown(function(event) {
                                if (event.keyCode == 13) {
                                    $("#search-categories").click();
                                    return false;
                                }
                            });

                            $("#@Html.FieldIdFor(model => model.SearchCategoryName)").keydown(function(event) {
                                if (event.keyCode == 13) {
                                    $("#search-categories").click();
                                    return false;
                                }
                            });

                            $("#@Html.FieldIdFor(model => model.SearchEquipmentName)").keydown(function(event) {
                                if (event.keyCode == 13) {
                                    $("#search-categories").click();
                                    return false;
                                }
                            });

                        });

                        function additionalData() {
                            var data = {
                                SearchCreatedFrom: $('#@Html.FieldIdFor(model => model.SearchCreatedFrom)').val(),
                                SearchCreatedTo: $('#@Html.FieldIdFor(model => model.SearchCreatedTo)').val(),
                                OperationLevel:@Model.OperationLevel,
                                SearchPNo: $('#@Html.FieldIdFor(model => model.SearchPNo)').val(),
                                SearchSNo: $('#@Html.FieldIdFor(model => model.SearchSNo)').val(),
                                SearchPlaceName: $('#@Html.FieldIdFor(model => model.SearchPlaceName)').val(),
                                SearchCategoryName: $('#@Html.FieldIdFor(model => model.SearchCategoryName)').val(),
                                SearchEquipmentName: $('#@Html.FieldIdFor(model => model.SearchEquipmentName)').val(),
                                SearchDepartId: $('#@Html.FieldIdFor(model => model.SearchDepartId)').val(),
                                SearchUserId: $('#@Html.FieldIdFor(model => model.SearchUserId)').val(),
                                SearchResponsibleDepartmentId: $('#@Html.FieldIdFor(model => model.SearchResponsibleDepartmentId)').val(),
                                SearchResponsiblePersonId: $('#@Html.FieldIdFor(model => model.SearchResponsiblePersonId)').val(),
                                SearchMaintenancePersonId: $('#@Html.FieldIdFor(model => model.SearchMaintenancePersonId)').val(),
                                SearchIsExternalId: $('#@Html.FieldIdFor(model => model.SearchIsExternalId)').val(),
                                SearchState: $('#@Html.FieldIdFor(model => model.SearchState)').val()
                            };
                            addAntiForgeryToken(data);
                            return data;
                        }
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
}